Repository navigation
Implement the non-generic IComparer on NaturalStringComparer [minor] - #75
Open
matt-edmondson wants to merge 1 commit into
Open
matt-edmondson wants to merge 1 commit into
matt-edmondson wants to merge 1 commit into
Conversation
ArrayList.Sort, Array.Sort(Array, IComparer), WPF's CustomSort and the WinForms sorters accept only System.Collections.IComparer, so callers had to write their own adapter. An explicit IComparer.Compare now forwards to the generic Compare, orders nulls the same way, and throws ArgumentException for arguments that aren't strings, as Comparer<T> does. The generic Compare is unchanged and still allocates nothing. Fixes #74 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3WL4nzXCqdxvXj85f
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #74
What changed
NaturalStringComparernow implementsSystem.Collections.ICompareras well asIComparer<string?>. Callers can pass it toArrayList.Sort,Array.Sort(Array, IComparer), WPF'sListCollectionView.CustomSort, and the WinForms sorters without writing an adapter.int IComparer.Compare(object?, object?)that forwards to the genericCompare. Null ordering and the ordinal tie-break therefore match the generic method.nullthrowArgumentException, asComparer<T>does. Anullpaired with anything follows the generic null rules, which is also whatComparer<T>does.Compareis unchanged.Compare_AllocatesNothingstill passes.Tests
There are four new tests in
NaturalStringComparerTests:ArrayList.Sortwith a null elementArray.Sort(object[], IComparer), including a leading-zero tieCompareover a grid of values that covers nulls, leading zeros, non-ASCII digits, and caseArgumentExceptionfor non-string argumentsThe tests look up the interface at run time. With the fix reverted they still build, and all 4 fail. With the fix applied, all 31 tests pass.
Package validation
dotnet pack -c Releasestill fails locally, but the CP* errors are the same 152 with and without this change. They are the Polyfill 11.4.5 shim drift tracked by #72, and #73 fixes them. Adding an interface introduces no new compatibility errors.Generated by Claude Code